<template>
  <div class="com-container">
    <div class="com-chart" ref="stock_ref"></div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  data () {
    return {
      chartInstance: null,
      allData: null,
      currIndex: 0
    }
  },
  created () { // 组件创建后 - 函数
    // 组件创建时，注册socket的回调函数
    this.$socket.registerCallBack('stockData', this.getData)
  },
  mounted () {
    this.initChart()
    // this.getData()
    // 发送数据给服务器，告诉服务器，这个客户端需要数据
    this.$socket.send({
      action: 'getData',
      socketType: 'stockData',
      chartName: 'stock',
      value: ''
    })
    this.resizeChart()
    window.addEventListener('resize', this.resizeChart)
  },
  destoryed () {
    // 移除监听
    window.removeEventListener('resize', this.resizeChart)
    // 移除定时器
    this.removeInterval()
    // 组件销毁时，注销socket的回调函数
    this.$socket.unregisterCallBack('stockData')
  },
  methods: {
    // 1.初始化echarts对象
    initChart () {
      this.chartInstance = this.$echarts.init(this.$refs.stock_ref, this.theme)
      const initOption = {
        title: {
          text: '▎库存销售分析',
          left: 20,
          top: 20
        }
      }
      this.chartInstance.setOption(initOption)
      this.chartInstance.on('mouseover', () => {
        this.removeInterval()
      })
      this.chartInstance.on('mouseout', () => {
        this.startInterval()
      })
    },

    // 2.获取数据
    getData (res) {
      // 获取数据
      // const { data: res } = await this.$http.get('stock')
      this.allData = res
      this.updateChart()
      this.startInterval()
    },

    // 3.更新数据
    updateChart () {
      const centerArr = [
        ['18%', '40%'],
        ['50%', '40%'],
        ['82%', '40%'],
        ['34%', '75%'],
        ['66%', '75%']
      ]
      const colorArr = [
        ['#4FF778', '#0BA82C'],
        ['#E5DD45', '#E8B11C'],
        ['#E8821C', '#E55445'],
        ['#5052EE', '#AB6EE5'],
        ['#23E5E5', '#2E72BF']
      ]
      // 处理图表需要的数据
      const start = this.currIndex * 5
      const end = (this.currIndex + 1) * 5
      const showData = this.allData.slice(start, end)
      const seriesArr = showData.map((item, index) => {
        return {
          type: 'pie',
          center: centerArr[index],
          hoverAnimation: false,
          labelLine: {
            show: false
          },
          label: {
            position: 'center',
            color: colorArr[index][0]
          },
          data: [
            {
              name: item.name + '\n' + '\n' + item.sales,
              value: item.sales,
              itemStyle: {
                color: new this.$echarts.graphic.LinearGradient(0, 1, 0, 0, [
                  {
                    offset: 0,
                    color: colorArr[index][0]
                  },
                  {
                    offset: 1,
                    color: colorArr[index][1]
                  }
                ])
              }
            },
            {
              value: item.stock,
              itemStyle: {
                color: '#333843'
              }
            }
          ]
        }
      })
      const dataOption = {
        series: seriesArr
      }
      this.chartInstance.setOption(dataOption)
    },

    // 4.分辨率适配
    resizeChart () {
      const titleFontSize = this.$refs.stock_ref.offsetWidth / 100 * 3.6
      const innerRadius = titleFontSize * 2.8
      const outterRadius = innerRadius * 1.125
      const resizeOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize
          }
        },
        series: [
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 1.5
            }
          },
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2
            }
          },
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2
            }
          },
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2
            }
          },
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2
            }
          }
        ]
      }
      this.chartInstance.setOption(resizeOption)
      this.chartInstance.resize()
    },

    // 5.定时切换
    startInterval () {
      if (this.timeId) {
        this.removeInterval()
      }
      this.timeId = setInterval(() => {
        this.currIndex++
        if (this.currIndex > this.allData.length / 5 - 1) {
          this.currIndex = 0
        }
        this.updateChart()
      }, 5000)
    },

    // 6.清除定时器
    removeInterval () {
      clearInterval(this.timeId)
    }
  },
  computed: {
    ...mapState(['theme'])
  },
  watch: {
    theme () {
      console.log('主题切换了')
      // 1.销毁当前图表
      this.chartInstance.dispose()
      // 2.重新注册当前图表 -- 以最新主题渲染图表
      this.initChart()
      this.resizeChart()
      this.updateChart()
    }
  }
}
</script>

<style lang="less" scoped>
</style>
